'use strict';

CustomerModule.controller('CustomerController',
    function CustomerController($scope, customer, $location, Map) {
	$scope.customer={
			name:customer.name,
			address:customer.address
	};
  $scope.customerName = customer.name;
  $scope.customerAddress = customer.address;


  $scope.findRestaurants = function(customerName, customerAddress) {
    customer.name = customerName;
    customer.address = customerAddress;

    $location.url('/');
  };
  
	var map = Map.initMap();
	Map.locateMe(function(pos){
		$scope.customer=Map.getAddress(pos.ob,pos.pb, function (data){console.log(data.formatted_address)});
	});
	var marker = null;
	var circle = null;
	Map.addListener(map, 'click', function(event) {
		if(marker == null){
		   	marker = Map.addMarker(event.latLng,'Click to delete','common/img/store.png');
		   	Map.addListener(marker, 'click',function(event1){
		   		Map.removeFeature(marker);Map.removeFeature(circle);
		   		marker = null;
		   	});
		   	circle = Map.addCircle(map,event.latLng,750, true, '#ff0000');
		   	Map.addListener(circle, 'center_changed', function (event1){
		   		marker.setPosition(circle.getCenter());
		   		marker.setAnimation(google.maps.Animation.DROP);
		   	});
		}
		else{
			// When circle center is modified, the associated event is called, 
			// hence marker position is updated
			circle.setCenter(event.latLng);
		}
		
	});
	
});
